package com.indusfo.spc.service.impl;


import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.*;
import com.indusfo.spc.pojo.*;
import com.indusfo.spc.service.MatVerService;
import org.apache.commons.lang3.StringUtils;
import com.indusfo.spc.vo.HintMessage;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author liub
 * @Date 2019/11/20 9:38
 * @Version 1.0
 */
@Service
public class MatVerServiceImpl implements MatVerService {


    // 声明日志的静态变量
    private static final Logger logger = LoggerFactory.getLogger(MatVerServiceImpl.class);

    @Resource
    private MatVerMapper matVerMapper;

    @Resource
    private MatFlowMapper matFlowMapper;

    @Resource
    private MatRepairMapper matRepairMapper;
    @Resource
    private ProStationMapper proStationMapper;

    @Resource
    private MatStationMapper matStationMapper;

    @Resource
    private MatDeteMapper matDeteMapper;
    @Resource
    private MatBomMapper matBomMapper;
    @Resource
    private MatBadphenoMapper matBadphenoMapper;
    @Resource
    private MatRepairFlowMapper matRepairFlowMapper;
    @Resource
    private MatRepairDeteMapper matRepairDeteMapper;

    @Resource
    private RepairDetailMapper repairDetailMapper;
    @Resource
    private BadphenoProMapper badphenoProMapper;

    @Resource
    private UploadMapper uploadMapper;
    //查询
    @Override
    public JSONObject queryMatVer(MatVer matVer) {
        try {
            //获取页面的数据个数
            Integer pagesize = matVer.getPagesize();
            //获取查询的页数
            Integer pageindex = matVer.getPageindex();
            if (pagesize != null && pageindex != null) {
                matVer.setIncept(pagesize * (pageindex - 1));
            }
            //执行查询操作
            List<MatVer> matVerList = matVerMapper.queryMatVer(matVer);

            //如果页数和页面数量都不为空的情况下进行计数
            if (matVerList.isEmpty()) {
                return JSONObject.oK(HintMessage.QUERY_NULL, matVerList, 0);
            } else {
                Integer count = matVerMapper.countMatVer(matVer);
                return JSONObject.oK(HintMessage.QUERY_SUCCESS, matVerList, count);
            }

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }
    }

    //新增
    @Override
    public JSONObject insertMatVer(MatVer matVer) {
        try {
            if (matVer.getMatVerId() != null) {
                throw new ModifyFailedException(HintMessage.INSERT_ID);
            }
            //初始化基础数据
            if (matVer.getDataState() == null) {
                matVer.setDataState(1);
            }
            //判重
            MatVer matVer1=new MatVer();
			matVer1.setMatId(matVer.getMatId());
            matVer1.setMatVerCode(matVer.getMatVerCode());
            List<MatVer> matVerList = matVerMapper.queryMatVer(matVer1);

            if(matVerList.size()!=0){
                matVer.setMatVerId( matVerList.get(0).getMatVerId());
                //判重
                checkParam(matVer);
                //执行修改操作
                int row = matVerMapper.updateMatVer(matVer);
                if (row == 0) {
                    throw new ModifyFailedException(HintMessage.UPDATE_DEFEATED);
                }
                return JSONObject.oK(HintMessage.UPDATE_SUCCESS,matVer.getMatVerId());
            }else {
                checkParam(matVer);
                //执行新增操作
                Integer row = matVerMapper.insertMatVer(matVer);
                if (row == 0) {
                    throw new ModifyFailedException(HintMessage.INSERT_DEFEATED);
                }
                return JSONObject.oK(HintMessage.INSERT_SUCCESS, matVer.getMatVerId());
            }




        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    //修改
    @Override
    public JSONObject updateMatVer(MatVer matVer) {
        try {
            if (matVer.getMatVerId() == null) {
                throw new ParamsErrorException(HintMessage.UPDATE_ID);
            }
            //初始化基础数据
            if (matVer.getDataState() == null) {
                matVer.setDataState(1);
            }
            //判重
            checkParam(matVer);
            //执行修改操作
            int row = matVerMapper.updateMatVer(matVer);
            if (row == 0) {
                throw new ModifyFailedException(HintMessage.UPDATE_DEFEATED);
            }
            return JSONObject.oK(HintMessage.UPDATE_SUCCESS);
        } catch (GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    //删除
    @Override
    public JSONObject deleteMatVer(List<Integer> matVerIds, Integer dataState) {
        try {
            //空值判断
            if (dataState == null) {
                throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
            }
            if (matVerIds == null) {
                throw new ParamsErrorException(HintMessage.LIST_NULL);
            }
            //记录异常信息
            String msg = "";
            switch (dataState) {
                case 1:
                    msg = HintMessage.START_VALUE;
                    break;
                case 2:
                    msg = HintMessage.DELETE_VALUE;
                    break;
                case 3:
                    msg = HintMessage.STOP_VALUE;
                    break;
                default:
                    throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
            }
            //执行删除操作
            int row = matVerMapper.deleteMatVer(matVerIds, dataState);

            for (Integer matVerId : matVerIds) {

                //级联执行工序流程删除
                List<MatFlow> matFlowList = matFlowMapper.queryMatFlowByMatVerId(matVerId);
                if (matFlowList != null && !matFlowList.isEmpty()) {
                    for (MatFlow matFlow : matFlowList) {
                        Integer matFlowId = matFlow.getMatFlowId();
                        matFlowMapper.deleteMatFlowByMatFlowId(matFlowId, dataState);

                        //级联执行工位删除
                        List<MatStation> matStationList = matStationMapper.queryMatStationByMatFlowId(matFlowId);
                        if (matStationList != null && !matStationList.isEmpty()) {
                            for (MatStation matStation : matStationList) {
                                matStationMapper.deleteMatStationByMatStationId(matStation.getMatStationId(), dataState);
                            }
                        }

                        //级联执行采集参数删除
                        List<MatDete> matDeteList = matDeteMapper.queryMatDeteByMatFlowId(matFlowId);
                        if (matDeteList != null && !matDeteList.isEmpty()) {
                            for (MatDete matDete : matDeteList) {
                                matDeteMapper.deleteMatDeteByMatDeteId(matDete.getMatDeteId(), dataState);
                            }
                        }

                        //级联执行关键物料删除
                        List<MatBom> matBomList = matBomMapper.queryMatBomByMatFlowId(matFlowId);
                        if (matBomList != null && !matBomList.isEmpty()) {
                            for (MatBom matBom : matBomList) {
                                matBomMapper.deleteMatBomByMatBomId(matBom.getMatBomId(), dataState);
                            }
                        }

                        //级联执行不良现象删除
                        List<MatBadpheno> matBadphenoList = matBadphenoMapper.queryMatBadphenoByMatFlowId(matFlowId);
                        if (matBadphenoList != null && !matBadphenoList.isEmpty()) {
                            for (MatBadpheno matBadpheno : matBadphenoList) {
                                matBadphenoMapper.deleteMatBadphenoByMatBadphenoId(matBadpheno.getMatBadphenoId(), dataState);
                            }
                        }
                    }
                }

                //级联执行维修流程删除
                List<MatRepair> matRepairList = matRepairMapper.queryMatRepairByMatVerId(matVerId);
                if (matRepairList != null && !matRepairList.isEmpty()) {
                    for (MatRepair matRepair : matRepairList) {
                        Integer matRepairId = matRepair.getMatRepairId();
                        matRepairMapper.deleteMatRepairByMatRepairId(matRepairId, dataState);

                        //级联执行工序删除
                        List<MatRepairFlow> matRepairFlowList = matRepairFlowMapper.queryMatRepairFlowByMatRepairId(matRepairId);
                        if (matRepairFlowList != null && !matRepairFlowList.isEmpty()) {
                            for (MatRepairFlow matRepairFlow : matRepairFlowList) {
                                matRepairFlowMapper.deleteMatRepairFlowByMatRepairFlowId(matRepairFlow.getMatRepairFlowId(), dataState);
                            }
                        }

                        //级联执行维修采集参数删除
                        List<MatRepairDete> matRepairDeteList = matRepairDeteMapper.queryMatRepairDeteByMatRepairId(matRepairId);
                        if (matRepairDeteList != null && !matRepairDeteList.isEmpty()) {
                            for (MatRepairDete matRepairDete : matRepairDeteList) {
                                matRepairDeteMapper.deleteMatRepairDeteByMatRepairDeteId(matRepairDete.getMatRepairDeteId(), dataState);
                            }
                        }
                    }
                }

            }

            //声明msg字符串，用来存抛出的异常
            if (row == 0) {
                msg = msg + HintMessage.DEFEATED_VALUE;
            } else {
                msg = msg + HintMessage.SUCCESS_VALUE;
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 级联删除物料版本下的工序流程和维修流程
     */
    @Transactional
    @Override
    public JSONObject deleteCascadeMatRepair(List<Integer> matVerIds, Integer dataState) {

        //空值判断
        if (dataState == null) {
            throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
        }
        if (matVerIds == null) {
            throw new ParamsErrorException(HintMessage.LIST_NULL);
        }
        //记录异常信息
        String msg = "";
        switch (dataState) {
            case 1:
                msg = HintMessage.START_VALUE;
                break;
            case 2:
                msg = HintMessage.DELETE_VALUE;
                break;
            case 3:
                msg = HintMessage.STOP_VALUE;
                break;
            default:
                throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
        }
        //执行删除操作
        int row = matVerMapper.deleteMatVer(matVerIds, dataState);

        //根据传入的matRepairIds查询对应的工序和维修采集参数
        for (Integer matVerId : matVerIds) {

            //级联执行工序流程删除
            List<MatFlow> matFlowList = matFlowMapper.queryMatFlowByMatVerId(matVerId);
            if (matFlowList != null && !matFlowList.isEmpty()) {
                for (MatFlow matFlow : matFlowList) {
                    Integer matFlowId = matFlow.getMatFlowId();
                    matFlowMapper.deleteMatFlowByMatFlowId(matFlowId, dataState);

                    //级联执行工位删除
                    List<MatStation> matStationList = matStationMapper.queryMatStationByMatFlowId(matFlowId);
                    if (matStationList != null && !matStationList.isEmpty()) {
                        for (MatStation matStation : matStationList) {
                            matStationMapper.deleteMatStationByMatStationId(matStation.getMatStationId(), dataState);
                        }
                    }

                    //级联执行采集参数删除
                    List<MatDete> matDeteList = matDeteMapper.queryMatDeteByMatFlowId(matFlowId);
                    if (matDeteList != null && !matDeteList.isEmpty()) {
                        for (MatDete matDete : matDeteList) {
                            matDeteMapper.deleteMatDeteByMatDeteId(matDete.getMatDeteId(), dataState);
                        }
                    }

                    //级联执行关键物料删除
                    List<MatBom> matBomList = matBomMapper.queryMatBomByMatFlowId(matFlowId);
                    if (matBomList != null && !matBomList.isEmpty()) {
                        for (MatBom matBom : matBomList) {
                            matBomMapper.deleteMatBomByMatBomId(matBom.getMatBomId(), dataState);
                        }
                    }

                    //级联执行不良现象删除
                    List<MatBadpheno> matBadphenoList = matBadphenoMapper.queryMatBadphenoByMatFlowId(matFlowId);
                    if (matBadphenoList != null && !matBadphenoList.isEmpty()) {
                        for (MatBadpheno matBadpheno : matBadphenoList) {
                            matBadphenoMapper.deleteMatBadphenoByMatBadphenoId(matBadpheno.getMatBadphenoId(), dataState);
                        }
                    }
                }
            }

            //级联执行维修流程删除
            List<MatRepair> matRepairList = matRepairMapper.queryMatRepairByMatVerId(matVerId);
            if (matRepairList != null && !matRepairList.isEmpty()) {
                for (MatRepair matRepair : matRepairList) {
                    Integer matRepairId = matRepair.getMatRepairId();
                    matRepairMapper.deleteMatRepairByMatRepairId(matRepairId, dataState);

                    //级联执行工序删除
                    List<MatRepairFlow> matRepairFlowList = matRepairFlowMapper.queryMatRepairFlowByMatRepairId(matRepairId);
                    if (matRepairFlowList != null && !matRepairFlowList.isEmpty()) {
                        for (MatRepairFlow matRepairFlow : matRepairFlowList) {
                            matRepairFlowMapper.deleteMatRepairFlowByMatRepairFlowId(matRepairFlow.getMatRepairFlowId(), dataState);
                        }
                    }

                    //级联执行维修采集参数删除
                    List<MatRepairDete> matRepairDeteList = matRepairDeteMapper.queryMatRepairDeteByMatRepairId(matRepairId);
                    if (matRepairDeteList != null && !matRepairDeteList.isEmpty()) {
                        for (MatRepairDete matRepairDete : matRepairDeteList) {
                            matRepairDeteMapper.deleteMatRepairDeteByMatRepairDeteId(matRepairDete.getMatRepairDeteId(), dataState);
                        }
                    }
                }
            }

        }

        //声明msg字符串，用来存抛出的异常
        if (row == 0) {
            msg = msg + HintMessage.DEFEATED_VALUE;
        } else {
            msg = msg + HintMessage.SUCCESS_VALUE;
        }
        return JSONObject.oK(msg);

    }

    /**
     * @Author 熊冰 保存物料流程
     * @Date 2019/11/23 10:45
     */
    @Transactional
    @Override
    public JSONObject saveMatFlow(MatVer matVer) {

        //判空
        if (matVer == null) {
            return JSONObject.build(JSONObject.ERROR, HintMessage.DATA_NULL);
        }
        if (matVer.getMatVerId() == null) {
            return JSONObject.build(JSONObject.ERROR, HintMessage.MATVERID_NULL);
        }

        //获取版本
        Integer matVerId = matVer.getMatVerId();
        //获取list集合
        List<MatFlow> matFlowList = new ArrayList<>();
        matFlowList = matVer.getMatFlowList();
        for (MatFlow matFlow : matFlowList) {
            matFlow.setMatVerId(matVerId);
        }

        //获取数据库数据
        MatFlow matFlow1 = new MatFlow();
        matFlow1.setMatVerId(matVerId);

        List<MatFlow> oldmatFlowList = matFlowMapper.queryMatFlow(matFlow1);
        //删除
        List<Integer> deleteList = new ArrayList<>();
        List<Integer> oldIds = new ArrayList<>();
        List<Integer> newIds = new ArrayList<>();
        if (oldmatFlowList != null) {
            for (MatFlow matFlow : oldmatFlowList) {
                oldIds.add(matFlow.getMatFlowId());
            }
        }
        if (matFlowList != null) {
            for (MatFlow matFlow : matFlowList) {
                newIds.add(matFlow.getMatFlowId());
            }
        }
        HashSet h1 = new HashSet(oldIds);
        HashSet h2 = new HashSet(newIds);
        h1.removeAll(h2);
        deleteList.addAll(h1);
        int deleterow = 0;
        int insertrow = 0;
        int updaterow = 0;
        if (deleteList.size() != 0) {
            deleterow = matFlowMapper.deleteMatFlow(deleteList, 2);

            //根据传入的matFlowIds查询对应的工位、采集参数、关键物料、不良现象集合
            for (Integer matFlowId : deleteList) {
                //级联执行工位删除
                List<MatStation> matStationList = matStationMapper.queryMatStationByMatFlowId(matFlowId);
                if (matStationList != null && !matStationList.isEmpty()) {
                    for (MatStation matStation : matStationList) {
                        matStationMapper.deleteMatStationByMatStationId(matStation.getMatStationId(), 2);
                    }
                }

                //级联执行采集参数删除
                List<MatDete> matDeteList = matDeteMapper.queryMatDeteByMatFlowId(matFlowId);
                if (matDeteList != null && !matDeteList.isEmpty()) {
                    for (MatDete matDete : matDeteList) {
                        matDeteMapper.deleteMatDeteByMatDeteId(matDete.getMatDeteId(), 2);
                    }
                }

                //级联执行关键物料删除
                List<MatBom> matBomList = matBomMapper.queryMatBomByMatFlowId(matFlowId);
                if (matBomList != null && !matBomList.isEmpty()) {
                    for (MatBom matBom : matBomList) {
                        matBomMapper.deleteMatBomByMatBomId(matBom.getMatBomId(), 2);
                    }
                }

                //级联执行不良现象删除
                List<MatBadpheno> matBadphenoList = matBadphenoMapper.queryMatBadphenoByMatFlowId(matFlowId);
                if (matBadphenoList != null && !matBadphenoList.isEmpty()) {
                    for (MatBadpheno matBadpheno : matBadphenoList) {
                        matBadphenoMapper.deleteMatBadphenoByMatBadphenoId(matBadpheno.getMatBadphenoId(), 2);
                    }
                }

                //级联执行维修流程工序删除
                List<MatRepairFlow> matRepairFlowList = matRepairFlowMapper.queryMatRepairFlowByMatFlowId(matFlowId);
                if (matRepairFlowList != null && !matRepairFlowList.isEmpty()) {
                    for (MatRepairFlow matRepairFlow : matRepairFlowList) {
                        matRepairFlowMapper.deleteMatRepairFlowByMatRepairFlowId(matRepairFlow.getMatRepairFlowId(), 2);
                    }
                }
            }
        }
        //删除完成  进行新增 和 修改
        if (matFlowList != null) {
            for (MatFlow matFlow : matFlowList
            ) {
                if (matFlow.getDataState() == null) {
                    matFlow.setDataState(1);
                }
                if (matFlow.getSequence() == null) {
                    matFlow.setSequence(1);
                }

                if (matFlow.getMatFlowId() != null) {
                    if (matFlow.getMatFlowId().equals(0)) {
                        matFlow.setMatFlowId(null);
                        insertrow = insertrow + matFlowMapper.insertMatFlow(matFlow);
                        //带出  工位和不良
                        Integer newFlow = matFlow.getMatFlowId();
                        Integer proId = matFlow.getProId();

                        //查询工序下工位
                        Pro pro = new Pro();
                        pro.setProId(proId);

                        List<ProStation> proStations = proStationMapper.queryByProId(proId);
                        //带出  工位
                        for (int i = 0; i < proStations.size(); i++) {
                            MatStation matStation = new MatStation();
                            matStation.setMatFlowId(newFlow);
                            matStation.setStationId(proStations.get(i).getStationId());
                            matStation.setDataState(1);
                            matStationMapper.insertMatStation(matStation);
                        }

                        //带出  工位和不良  完成
                        List<BadphenoPro> badphenoPros = badphenoProMapper.queryByProId(proId);
                        //带出  工位
                        for (int i = 0; i < badphenoPros.size(); i++) {
                            MatBadpheno matBadpheno = new MatBadpheno();
                            matBadpheno.setMatFlowId(newFlow);
                            matBadpheno.setBadphenoId(badphenoPros.get(i).getBadphenoId());
                            matBadpheno.setDataState(1);
                            matBadphenoMapper.insertMatBadpheno(matBadpheno);
                        }


                    } else {
                        updaterow = updaterow + matFlowMapper.updateMatFlow(matFlow);
                    }
                }
            }
        }
        return JSONObject.oK(HintMessage.SAVE_SUCCESS, deleterow + insertrow + updaterow);

    }

    @Transactional
    @Override
    public JSONObject saveMatRepair(MatVer matVer) {
        try {
            //判空
            if (matVer == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (matVer.getMatVerId() == null) {
                throw new ParamsErrorException(HintMessage.MATVERID_NULL);
            }

            //获取版本
            Integer matVerId = matVer.getMatVerId();
            //获取list集合
            List<MatRepair> matRepairList = new ArrayList<>();
            matRepairList = matVer.getMatRepairList();
            for (MatRepair matRepair : matRepairList) {
                matRepair.setMatVerId(matVerId);
            }


            //获取数据库数据
            MatRepair matRepair1 = new MatRepair();
            matRepair1.setMatVerId(matVerId);

            List<MatRepair> oldmatRepairList = matRepairMapper.queryMatRepair(matRepair1);
            //删除
            List<Integer> deleteList = new ArrayList<>();
            List<Integer> oldIds = new ArrayList<>();
            List<Integer> newIds = new ArrayList<>();
            if (oldmatRepairList != null) {
                for (MatRepair matRepair : oldmatRepairList) {
                    oldIds.add(matRepair.getMatRepairId());
                }
            }
            if (matRepairList != null) {
                for (MatRepair matRepair : matRepairList) {
                    newIds.add(matRepair.getMatRepairId());
                }
            }
            HashSet h1 = new HashSet(oldIds);
            HashSet h2 = new HashSet(newIds);
            h1.removeAll(h2);
            deleteList.addAll(h1);
            int deleterow = 0;
            int insertrow = 0;
            int updaterow = 0;
            if (deleteList.size() != 0) {
                deleterow = matRepairMapper.deleteMatRepair(deleteList, 2);
                //根据传入的matRepairIds查询对应的工序和维修采集参数
                for (Integer matRepairId : deleteList) {
                    //级联执行工序删除
                    List<MatRepairFlow> matRepairFlowList = matRepairFlowMapper.queryMatRepairFlowByMatRepairId(matRepairId);
                    if (matRepairFlowList != null && !matRepairFlowList.isEmpty()) {
                        for (MatRepairFlow matRepairFlow : matRepairFlowList) {
                            matRepairFlowMapper.deleteMatRepairFlowByMatRepairFlowId(matRepairFlow.getMatRepairFlowId(), 2);
                        }
                    }

                    //级联执行维修采集参数删除
                    List<MatRepairDete> matRepairDeteList = matRepairDeteMapper.queryMatRepairDeteByMatRepairId(matRepairId);
                    if (matRepairDeteList != null && !matRepairDeteList.isEmpty()) {
                        for (MatRepairDete matRepairDete : matRepairDeteList) {
                            matRepairDeteMapper.deleteMatRepairDeteByMatRepairDeteId(matRepairDete.getMatRepairDeteId(), 2);
                        }
                    }
                }
            }
            //删除完成  进行新增 和 修改
            if (matRepairList != null) {
                for (MatRepair matRepair : matRepairList) {
                    if (matRepair.getDataState() == null) {
                        matRepair.setDataState(1);
                    }


                    if (matRepair.getMatRepairId() != null) {
                        if (matRepair.getMatRepairId().equals(0)) {
                            matRepair.setMatRepairId(null);
                            insertrow = insertrow + matRepairMapper.insertMatRepair(matRepair);


                        } else {
                            updaterow = updaterow + matRepairMapper.updateMatRepair(matRepair);
                        }
                    }
                }
            }
            return JSONObject.oK(HintMessage.SAVE_SUCCESS, deleterow + insertrow + updaterow);
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    @Override
    public JSONObject leadMatVer(Integer matId, Integer matVerId, Integer leadBom, Integer leadStation, Integer leadBadpheno, Integer leadDete, Integer leadRepairDete, Integer leadRepairFlow) {
        try {
            //校验非空
            if (matId == null) {
                throw new ParamsErrorException(HintMessage.MATID_NULL);
            }
            if (matVerId == null) {
                throw new ParamsErrorException(HintMessage.MATVERID_NULL);
            }
            leadBom = leadBom == null ? 0 : leadBom;
            leadStation = leadStation == null ? 0 : leadStation;
            leadBadpheno = leadBadpheno == null ? 0 : leadBadpheno;
            leadDete = leadDete == null ? 0 : leadDete;
            leadRepairDete = leadRepairDete == null ? 0 : leadRepairDete;
            leadRepairFlow = leadRepairFlow == null ? 0 : leadRepairFlow;
            //导入必导项
            //导入版本
            MatVer matVer = new MatVer();
            matVer.setMatVerId(matVerId);
            List<MatVer> matVerList = matVerMapper.queryMatVer(matVer);
            if (matVerList.size() != 0) {
                matVer = matVerList.get(0);
            }
            matVer.setMatVerId(null);
            matVer.setDataState(1);
            matVer.setMatId(matId);
            //判重
            if (matVerMapper.checkMatVer(matVer) != 0) {
                throw new ParamsErrorException(HintMessage.REPEATING_DATA);
            }
            //新增版本
            matVerMapper.insertMatVer(matVer);
            //获取新版本id
            Integer newMatVerId = matVer.getMatVerId();
            //导入工艺流程
            MatFlow matFlow = new MatFlow();
            matFlow.setMatVerId(matVerId);
            List<MatFlow> matFlowList = matFlowMapper.queryMatFlow(matFlow);
            if (matFlowList.size() != 0) {
                for (MatFlow matFlow1 : matFlowList) {
                    //保存老流程id
                    Integer oldMatFlowId = matFlow1.getMatFlowId();
                    //创建新流程
                    MatFlow newMatFlow = matFlow1;
                    newMatFlow.setMatFlowId(null);
                    newMatFlow.setDataState(1);
                    newMatFlow.setMatVerId(newMatVerId);
                    //新增流程
                    matFlowMapper.insertMatFlow(newMatFlow);
                    //保存新流程id
                    Integer newMatFlowId = newMatFlow.getMatFlowId();

                    //导入工位
                    if (leadStation != 0) {
                        //获取标准流程中的数据
                        MatStation matStation = new MatStation();
                        matStation.setMatFlowId(oldMatFlowId);
                        List<MatStation> matStationList = matStationMapper.queryMatStation(matStation);
                        //导入数据
                        for (MatStation matStation1 : matStationList) {
                            matStation1.setMatFlowId(newMatFlowId);
                            matStation1.setMatStationId(null);
                            matStationMapper.insertMatStation(matStation1);
                        }
                    }
                    //导入采集项目
                    if (leadDete != 0) {
                        //获取标准流程中的数据
                        MatDete matDete = new MatDete();
                        matDete.setMatFlowId(oldMatFlowId);
                        List<MatDete> matDeteList = matDeteMapper.queryMatDete(matDete);
                        //导入数据
                        for (MatDete matDete1 : matDeteList) {
                            matDete1.setMatFlowId(newMatFlowId);
                            matDete1.setMatDeteId(null);
                            matDeteMapper.insertMatDete(matDete1);
                        }
                    }
                    //导入关键物料
                    if (leadBom != 0) {
                        //获取标准流程中的数据
                        MatBom matBom = new MatBom();
                        matBom.setMatFlowId(oldMatFlowId);
                        List<MatBom> matBomList = matBomMapper.queryMatBom(matBom);
                        //导入数据
                        for (MatBom matBom1 : matBomList) {
                            matBom1.setMatFlowId(newMatFlowId);
                            matBom1.setMatBomId(null);
                            matBomMapper.insertMatBom(matBom1);
                        }
                    }
                    //导入不良现象
                    if (leadBadpheno != 0) {
                        //获取标准流程中的工位
                        MatBadpheno matBadpheno = new MatBadpheno();
                        matBadpheno.setMatFlowId(oldMatFlowId);
                        List<MatBadpheno> matBadphenoList = matBadphenoMapper.queryMatBadpheno(matBadpheno);
                        //导入工位
                        for (MatBadpheno matBadpheno1 : matBadphenoList) {
                            matBadpheno1.setMatFlowId(newMatFlowId);
                            matBadpheno1.setMatBadphenoId(null);
                            matBadphenoMapper.insertMatBadpheno(matBadpheno1);
                        }
                    }
                }
            }


            //导入维修流程
            MatRepair matRepair = new MatRepair();
            matRepair.setMatVerId(matVerId);
            List<MatRepair> matRepairList = matRepairMapper.queryMatRepair(matRepair);
            if (matRepairList.size() != 0) {
                for (MatRepair matRepair1 : matRepairList) {
                    //保存老流程id
                    Integer oldMatRepairId = matRepair1.getMatRepairId();
                    //创建新流程
                    MatRepair newMatRepair = matRepair1;
                    newMatRepair.setMatRepairId(null);
                    newMatRepair.setRepairId(matRepair1.getRepairId());
                    newMatRepair.setDataState(1);
                    newMatRepair.setMatVerId(newMatVerId);
                    //新增流程
                    matRepairMapper.insertMatRepair(newMatRepair);
                    //保存新流程id
                    Integer newMatRepairId = newMatRepair.getMatRepairId();

                    //导入维修流程 工序
                    if (leadRepairFlow != 0) {
                        MatRepairFlow matRepairFlow = new MatRepairFlow();
                        matRepairFlow.setMatRepairId(oldMatRepairId);
                        List<MatRepairFlow> matRepairFlowList = matRepairFlowMapper.queryMatRepairFlow(matRepairFlow);
                        //导入数据
                        for (MatRepairFlow matRepairFlow1 : matRepairFlowList) {
                            matRepairFlow1.setMatRepairId(newMatRepairId);
                            matRepairFlow1.setMatRepairFlowId(null);


                            for (MatFlow matFlow1:matFlowList    ) {
                                if(matFlow1.getSerialNum().equals(matRepairFlow1.getSerialNum())){
                                    matRepairFlow1.setMatFlowId(matFlow1.getMatFlowId());
                                }
                            }

                            matRepairFlowMapper.insertMatRepairFlow(matRepairFlow1);
                        }
                    }

                    //导入维修流程 采集项目

                    if (leadRepairDete != 0) {
                        //查询维修流程 下 所有 子工序
                        RepairDetail repairDetail = new RepairDetail();
                        repairDetail.setRepairId(matRepair1.getRepairId());
                        List<RepairDetail> repairDetailList = repairDetailMapper.selectAll(repairDetail);
                        for (RepairDetail repairDetail1 : repairDetailList) {
                            MatRepairDete matRepairDete = new MatRepairDete();
                            matRepairDete.setMatRepairId(oldMatRepairId);
                            matRepairDete.setRepairDetailId(repairDetail1.getRepairDetailId());
                            List<MatRepairDete> matRepairDeteList = matRepairDeteMapper.queryMatRepairDete(matRepairDete);
                            for (MatRepairDete matRepairDete1 : matRepairDeteList) {
                                matRepairDete1.setMatRepairId(newMatRepairId);
                                matRepairDete1.setMatRepairDeteId(null);
                                matRepairDeteMapper.insertMatRepairDete(matRepairDete1);
                            }

                        }
                    }


                }


            }


            return JSONObject.oK("导入成功", 0);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }

    }

    //判重
    public void checkParam(MatVer matVer) {
        String matVerCode = matVer.getMatVerCode();
        String matVerName = matVer.getMatVerName();
        Integer matId = matVer.getMatId();
        //物料版本编码判空
        if (StringUtils.isEmpty(matVerCode)) {
            throw new ParamsErrorException(HintMessage.CODE_NULL);
        }
        //物料版本名称判空
        if (StringUtils.isEmpty(matVerName)) {
            throw new ParamsErrorException(HintMessage.NAME_NULL);
        }
        //物料ID
        if (matId == null) {
            throw new ParamsErrorException(HintMessage.MATID_NULL);
        }
        //判断物料版本编码长度
        if (matVerCode.getBytes().length > 100) {
            throw new ParamsErrorException(HintMessage.CODE_SIZE);
        }
        //判断物料版本名称长度
        if (matVerName.getBytes().length > 100) {
            throw new ParamsErrorException(HintMessage.NAME_SIZE);
        }


        // 执行判重操作
        Integer Counts = matVerMapper.checkMatVer(matVer);
        if (Counts != 0) {
            throw new ParamsErrorException(HintMessage.REPEATING_DATA);
        }

    }

    /**
     * 工艺文件库查询
     */
    @Override
    public JSONObject queryMatVerByProfile(MatVer matVer) {
        try {
            //获取页面的数据个数
            Integer pagesize = matVer.getPagesize();
            //获取查询的页数
            Integer pageindex = matVer.getPageindex();
            if (pagesize != null && pageindex != null) {
                matVer.setIncept(pagesize * (pageindex - 1));
            }
            //执行查询操作
            List<MatVer> matVerList = matVerMapper.queryMatVerByProfile(matVer);

            //如果页数和页面数量都不为空的情况下进行计数
            if (matVerList.isEmpty()) {
                return JSONObject.oK(HintMessage.QUERY_NULL, matVerList, 0);
            } else {
                Integer count = matVerMapper.countMatVerByProfile(matVer);
                return JSONObject.oK(HintMessage.QUERY_SUCCESS, matVerList, count);
            }

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }
    }

    @Override
    public String checkExcelContent(UploadMatVer uploadMatVer) {
        //物料编号
        String matCode = uploadMatVer.getMatCode();
        //工艺版本名称
        String matVerName = uploadMatVer.getMatVerName();
        //导入相关工位
        String leadStation = uploadMatVer.getLeadStation();
        //导入相关采集项目
        String leadDete = uploadMatVer.getLeadDete();
        //导入相关关键无聊
        String leadBom = uploadMatVer.getLeadBom();
        //导入相关不良现象
        String leadBadpheno = uploadMatVer.getLeadBadpheno();
        //导入相关维修工序
        String leadRepairFlow = uploadMatVer.getLeadRepairFlow();
        //导入相关维修采集项目
        String leadRepairDete =uploadMatVer.getLeadRepairDete() ;
        if(StringUtils.isEmpty(matCode) && StringUtils.isEmpty(matVerName) && StringUtils.isEmpty(leadStation) && StringUtils.isEmpty(leadDete) &&
                StringUtils.isEmpty(leadBom) && StringUtils.isEmpty(leadBadpheno) && StringUtils.isEmpty(leadRepairFlow) && StringUtils.isEmpty(leadRepairDete)
        ){
            return "该行数据为空/删除不彻底";
        }
        if(StringUtils.isEmpty(matCode)){
            return "产品编码为空";
        }
        Map<String, Mat> matToProductMap = uploadMapper.queryMatToProduct();
        if(!matToProductMap.containsKey(matCode)){
            return "产品编码不存在/填写不规范";
        }
        uploadMatVer.setMatId(matToProductMap.get(matCode).getMatId());
        if(StringUtils.isEmpty(matVerName)){
            return "工艺版本名称为空";
        }
        Map<String, MatVer> uploadMatVerMap = uploadMapper.queryUploadMatVer();
        if(!uploadMatVerMap.containsKey(matVerName)){
            return "工艺版本名称不存在/填写不规范";
        }
        uploadMatVer.setMatVerId(uploadMatVerMap.get(matVerName).getMatVerId());
        uploadMatVer.setMatVerCode(uploadMatVerMap.get(matVerName).getMatVerCode());
        MatVer matVer=new MatVer();
        matVer.setMatId(matToProductMap.get(matCode).getMatId());
        matVer.setMatVerId(uploadMatVerMap.get(matVerName).getMatVerId());
        matVer.setMatVerCode(uploadMatVerMap.get(matVerName).getMatVerCode());
        if(uploadMapper.checkMatVer(matVer) != 0){
            return "所填写的产品对应版本已存在";
        }
        if(StringUtils.isEmpty(leadStation)){
            uploadMatVer.setLeadStation("0");
        }
        if(!StringUtils.isEmpty(checkNumber(leadStation))){
            return "导入相关工位"+checkNumber(leadStation);
        }
        if(StringUtils.isEmpty(leadDete)){
            uploadMatVer.setLeadStation("0");
        }
        if(!StringUtils.isEmpty(checkNumber(leadDete))){
            return "导入相关采集项目"+checkNumber(leadDete);
        }
        if(StringUtils.isEmpty(leadBom)){
            uploadMatVer.setLeadStation("0");
        }
        if(!StringUtils.isEmpty(checkNumber(leadBom))){
            return "导入相关关键物料"+checkNumber(leadBom);
        }
        if(StringUtils.isEmpty(leadBadpheno)){
            uploadMatVer.setLeadStation("0");
        }
        if(!StringUtils.isEmpty(checkNumber(leadBadpheno))){
            return "导入相关不良现象"+checkNumber(leadBadpheno);
        }
        if(StringUtils.isEmpty(leadRepairFlow)){
            uploadMatVer.setLeadStation("0");
        }
        if(!StringUtils.isEmpty(checkNumber(leadRepairFlow))){
            return "导入相关维修工序"+checkNumber(leadRepairFlow);
        }
        if(StringUtils.isEmpty(leadRepairDete)){
            uploadMatVer.setLeadStation("0");
        }
        if(!StringUtils.isEmpty(checkNumber(leadRepairDete))){
            return "导入相关维修采集项目"+checkNumber(leadRepairDete);
        }
        return null;
    }

    @Override
    @Transactional
    public JSONObject updateListUploadMatVer(List<UploadMatVer> uploadMatVerList) {
            //实现将每个集合中的每个对象的matverId重新赋值给oldMatVerId
// lamda表达式           uploadMatVerList.stream().forEach(item->item.setOldMatVerId(item.getMatVerId()));
            //将每个集合中的每个对象的matVerId去重提取出来然后生称新的集合
            List<Integer> oldMatVerId=new ArrayList<>();
// lamda表达式         oldMatVerId = uploadMatVerList.stream().map(UploadMatVer :: getMatVerId).collect(Collectors.toList());
            for (UploadMatVer uploadMatVer : uploadMatVerList) {
                uploadMatVer.setOldMatVerId(uploadMatVer.getMatVerId());
                oldMatVerId.add(uploadMatVer.getMatVerId());
            }
            //获取标准的工序流程   一个版本对应多个工序流程
            List<MatFlow> matFlowList = uploadMapper.queryMatFlowList(oldMatVerId);
            //旧的版本下的工序流程id
            List<Integer> oldMatFlowId=new ArrayList<>();
            for (MatFlow matFlow : matFlowList) {
                oldMatFlowId.add(matFlow.getMatFlowId());
            }
            //新的版本下的工序流程id
            List<Integer> newMatFlowId=new ArrayList<>();
            //标准流程对应版本下的工序流程下的工位
            List<MatStation> matStationList = uploadMapper.queryMatStationList(oldMatFlowId);
            //标准流程对应版本下的工序流程下的采集项目
            List<MatDete> matDeteList = uploadMapper.queryMatDeteList(oldMatFlowId);
            //标准流程对应版本下的工序流程下的关键物料
            List<MatBom> matBomList = uploadMapper.queryMatBomList(oldMatFlowId);
            //标准流程对应版本下的工序流程下的不良现象
            List<MatBadpheno> matBadphenoList = uploadMapper.queryMatBadphenoList(oldMatFlowId);
            //对应工序流程下的工位
            //获取标准版本的维修工序流程
            List<MatRepair> matRepairList = uploadMapper.queryMatRepairList(oldMatVerId);
            //获取标准版本的旧维修工序流程id
            List<Integer> oldMatRepirId=new ArrayList<>();
// lamda表达式         oldMatRepirId=matRepairList.stream().map(MatRepair::getMatRepairId).collect(Collectors.toList());
            //获取标准版本的旧repariId
            List<Integer> oldRepairId=new ArrayList<>();
// lamda表达式         oldRepairId=matRepairList.stream().map(MatRepair ::getMatRepairId).collect(Collectors.toList());
            for (MatRepair matRepair : matRepairList) {
                oldMatRepirId.add(matRepair.getMatRepairId());
                oldRepairId.add(matRepair.getRepairId());
                matRepair.setOldMatRepairId(matRepair.getMatRepairId());
            }
            //获取维修流程下的工序
            List<MatRepairFlow> matRepairFlowList=uploadMapper.queryMatRepairFlowList(oldMatRepirId);
            //获取维修流程下的维修采集项目下的工序名称
            List<RepairDetail> repairDetailList =uploadMapper.queryListRepairDetailList(oldRepairId);
            //查询维修流程下的维修采集项目明细的查询条件
            List<MatRepair> queryMatRepairDeteList =new ArrayList<>();
            /*  lamda表达式
            queryMatRepairDeteList=matRepairList.stream().
                                    filter(item -> !repairDetailList.stream().
                                    map(e -> e.getRepairId()).
                                    collect(Collectors.toList()).
                                    contains(item.getRepairId())).
                                    collect(Collectors.toList());*/

              for (RepairDetail repairDetail : repairDetailList) {
                    MatRepair matRepair=new MatRepair();
                    Integer repairDetailId = repairDetail.getRepairDetailId();
                    Integer repairId = repairDetail.getRepairId();
                    matRepair.setRepairId(repairDetailId);
                    for (MatRepair oldMatRepair : matRepairList) {
                        if(oldMatRepair.getRepairId().equals(repairId)){
                            matRepair.setMatRepairId(oldMatRepair.getMatRepairId());
                            break;
                        }
                    }
                    queryMatRepairDeteList.add(matRepair);
              }
            //查询维修流程下的维修采集项目明细
            List<MatRepairDete> matRepairDeteList=uploadMapper.queryMatRepairDeteList(queryMatRepairDeteList);
            //新的版本id
            List<Integer> newMatVerId=new ArrayList<>();
            //新增版本下的工序流程集合
            List<MatFlow> insertMatFlowList=new ArrayList<>();
            //新增版本下的维修工序流程集合
            List<MatRepair> insertMatRepairList=new ArrayList<>();
            //批量新增版本--获取对应的版本id  只需要将对应的版本id+物料id再新增一遍生成新的版本id
            if(!uploadMatVerList.isEmpty()){
                int matVerIdList = uploadMapper.insertMatVerList(uploadMatVerList);
            }
            for (int i = 0; i < oldMatVerId.size(); i++) {
                if(newMatVerId.size()<oldMatVerId.size()){
                    newMatVerId.add(uploadMatVerList.get(i).getMatVerId());
                }
            }
            //新增版本下的工序流程的条件
            for (MatFlow matFlow : matFlowList) {
                Integer matVerId = matFlow.getMatVerId();
                for (UploadMatVer uploadMatVer : uploadMatVerList) {
                    if(matVerId.equals(uploadMatVer.getOldMatVerId())){
                        matFlow.setNewMatVerId(uploadMatVer.getMatVerId());
                        break;
                    }
                }
                matFlow.setOldMatFlowId(matFlow.getMatFlowId());
                insertMatFlowList.add(matFlow);
            }
            //批量新增版本下的工序流程   一个产品对应的一个版本对应的一个工序流程  同时返回自增主键id
            if(!insertMatFlowList.isEmpty()){
                uploadMapper.insertMatFlowList(insertMatFlowList);
            }
            //新增版本下的维修流程的条件
            for (MatRepair matRepair : matRepairList) {
                Integer matVerId = matRepair.getMatVerId();
                for (UploadMatVer uploadMatVer : uploadMatVerList) {
                    if(matVerId.equals(uploadMatVer.getOldMatVerId())){
                        matRepair.setNewMatVerId(uploadMatVer.getMatVerId());
                        break;
                    }
                }
                insertMatRepairList.add(matRepair);
            }
            //批量新增版本下的维修流程
            if(!insertMatRepairList.isEmpty()) {
                uploadMapper.insertMatRepairList(insertMatRepairList);
            }
            //生成新的工序流程id集合
            for (MatFlow matFlow : insertMatFlowList) {
                newMatFlowId.add(matFlow.getMatFlowId());
            }
            //批量新增版本下工序流程下的工位 条件
            List<MatStation> insertMatStationList=new ArrayList<>();
            for (MatStation matStation : matStationList) {
                Integer matFlowId = matStation.getMatFlowId();
                for (MatFlow matFlow : insertMatFlowList) {
                    if(matFlowId.equals(matFlow.getOldMatFlowId())){
                        matStation.setNewMatFlowId(matFlow.getMatFlowId());
                        break;
                    }
                }
                insertMatStationList.add(matStation);
            }
            //批量新增版本下工序流程下的工位
            if(!insertMatStationList.isEmpty()){
                uploadMapper.insertMatStationList(insertMatStationList);
            }
            //批量新增版本下工序流程下的采集项目 条件
            List<MatDete> insertMatDeteList=new ArrayList<>();
            for (MatDete matDete : matDeteList) {
                Integer matFlowId = matDete.getMatFlowId();
                for (MatFlow matFlow : insertMatFlowList) {
                    if(matFlowId.equals(matFlow.getOldMatFlowId())){
                        matDete.setNewMatFlowId(matFlow.getMatFlowId());
                        break;
                    }
                }
                insertMatDeteList.add(matDete);
            }
            //批量新增版本下工序流程下的采集项目
            if(!insertMatDeteList.isEmpty()){
                uploadMapper.insertMatDeteList(insertMatDeteList);
            }
            //批量新增版本下工序流程的关键物料  条件
            List<MatBom> insertMatBomList=new ArrayList<>();
            for (MatBom matBom : matBomList) {
                Integer matFlowId = matBom.getMatFlowId();
                for (MatFlow matFlow : insertMatFlowList) {
                    if(matFlowId.equals(matFlow.getOldMatFlowId())){
                        matBom.setNewMatFlowId(matFlow.getMatFlowId());
                        break;
                    }
                }
                insertMatBomList.add(matBom);
            }
            //批量新增版本下工序流程的关键物料
            if(!insertMatBomList.isEmpty()){
                uploadMapper.insertMatBomList(insertMatBomList);
            }

            //批量新增版本下工序流程的不良现象 条件
            List<MatBadpheno> insertMatBadphenoList=new ArrayList<>();
            for (MatBadpheno matBadpheno : matBadphenoList) {
                Integer matFlowId = matBadpheno.getMatFlowId();
                for (MatFlow matFlow : insertMatFlowList) {
                    if(matFlowId.equals(matFlow.getOldMatFlowId())){
                        matBadpheno.setNewMatFlowId(matFlow.getMatFlowId());
                        break;
                    }
                }
                insertMatBadphenoList.add(matBadpheno);
            }
            //批量新增版本下工序流程的不良现象
            if(!insertMatBadphenoList.isEmpty()){
                uploadMapper.insertMatBadphenoList(insertMatBadphenoList);
            }
            //批量新增版本下维修流程的工序  条件
            List<MatRepairFlow> insertMatRepairFlowList=new ArrayList<>();
            for (MatRepairFlow matRepairFlow : matRepairFlowList) {
                Integer matFlowId = matRepairFlow.getMatFlowId();
                Integer matRepairId = matRepairFlow.getMatRepairId();
                for (MatFlow matFlow : insertMatFlowList) {
                    if(matFlowId.equals(matFlow.getOldMatFlowId())){
                        matRepairFlow.setMatFlowId(matFlow.getMatFlowId());
                        break;
                    }
                }
                for (MatRepair matRepair : insertMatRepairList) {
                    if(matRepairId.equals(matRepair.getOldMatRepairId())){
                        matRepairFlow.setMatRepairId(matRepair.getMatRepairId());
                        break;
                    }
                }
                insertMatRepairFlowList.add(matRepairFlow);
            }
            if(!insertMatRepairFlowList.isEmpty()){
                uploadMapper.insertMatRepairFlowList(insertMatRepairFlowList);
            }
            //批量新增版本下维修流程的维修采集项目
            //批量新增版本下的维修流程的维修采集项目  条件
            List<MatRepairDete> insertMatRepairDeteList=new ArrayList<>();
            for (MatRepairDete matRepairDete : matRepairDeteList) {
                Integer matRepairId = matRepairDete.getMatRepairId();
                for (MatRepair matRepair : insertMatRepairList) {
                    if(matRepairId.equals(matRepair.getOldMatRepairId())){
                        matRepairDete.setMatRepairId(matRepair.getMatRepairId());
                        break;
                    }
                }
                insertMatRepairDeteList.add(matRepairDete);
            }
            if(!insertMatRepairDeteList.isEmpty()){
                uploadMapper.insertMatRepairDeteList(insertMatRepairDeteList);
            }

            return JSONObject.oK("导入成功", insertMatFlowList,insertMatFlowList.size());

//            return JSONObject.oK("导入成功", uploadMatVerList,uploadMatVerList.size());

    }

    public String checkNumber(String param){
        if(!StringUtils.isBlank(param)){
            if(StringUtils.isNumeric(param)){
                if(Integer.parseInt(param)!=0 && Integer.parseInt(param)!=1){
                    return "不为指定数据0/1";
                }
            }else{
                return "不为指定数据0/1";
            }
        }
        return null;
    }
}
